{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 问题： 什么是元组拆包？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***元组拆包其实是从元组中提取出不同端的信息***，我们可以将这一操作理解为平行赋值。\n",
    "也就是说拆包会把一个可迭代对象中的元素一并赋值到对应变量中去。\n",
    "这样做可以让我们把注意力集中在我们所需要关注的元素上面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "beijing 2019 8.7\n"
     ]
    }
   ],
   "source": [
    "tuple = ('beijing','2019','8.7')\n",
    "city,year,date = tuple\n",
    "print (city,year,date)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拆包时我们还可以用 * 来处理剩下的元素，在 python 里常用这种方法来代表不确定数量参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "[2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "a, b, *rest = range(5)\n",
    "print(a) # 0\n",
    "print(b) # 1\n",
    "print(rest) # [2,3,包]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用占位符 _ 去忽略不需要关注的元素，相当于一个占位符，抵消掉了我们不需要关注的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "x, *_, y = (1, 2, 3, 4, 5)\n",
    "print(x) # 1\n",
    "print(y) # 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 问题：*args 和 **kwargs 的用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当函数的参数不确定时候可以用 *args 和 **kwargs。****args 是用于接收多余的未命名参数***，*****kwargs 用于接收实参中的命名参数***，\n",
    "其中 args 是一个元组类型，而 kwargs 是一个字典类型的数据。形参中的 *args 把传进来的数据放在了 args 这个元组中。\n",
    "把 **kwargs 传进来的数据放在了 kwargs 这个字典中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*args 其实就是把元组中的数据进行拆包，也就是把元组中的数据拆成单个数据\n",
      "0. apple\n",
      "1. banana\n",
      "2. cabbage\n",
      "**kwargs 就是把字典中的数据进行拆包，传进的命名数据可以从 kwargs 按键进行索引获得。\n"
     ]
    }
   ],
   "source": [
    "print (\"*args 其实就是把元组中的数据进行拆包，也就是把元组中的数据拆成单个数据\")\n",
    "\n",
    "def print_everything(*args):\n",
    "    for count, thing in enumerate(args):\n",
    "        print( '{0}. {1}'.format(count, thing))\n",
    "print_everything('apple', 'banana', 'cabbage')\n",
    "\n",
    "print(\"**kwargs 就是把字典中的数据进行拆包，传进的命名数据可以从 kwargs 按键进行索引获得。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple = fruit\n",
      "cabbage = vegetable\n",
      "如果同时使用 *args 和 **kwargs\n"
     ]
    }
   ],
   "source": [
    "def table_things(**kwargs):\n",
    "    for name, value in kwargs.items():\n",
    "       print( '{0} = {1}'.format(name, value))\n",
    "table_things(apple = 'fruit', cabbage = 'vegetable')\n",
    "\n",
    "print (\"如果同时使用 *args 和 **kwargs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "如果同时使用 *args 和 **kwargs\n"
     ]
    }
   ],
   "source": [
    "print (\"如果同时使用 *args 和 **kwargs\")\n",
    "\n",
    "def func(required_arg, *args, **kwargs):\n",
    "    # required_arg是一个常规参数\n",
    "    print (required_arg)\n",
    "\n",
    "    # args是一个元组\n",
    "    if args:\n",
    "        print (args)\n",
    "\n",
    "    # kwargs是一个字典，里面的键值就是传进来的参数变量名\n",
    "    if kwargs:\n",
    "        print (kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "required argument\n",
      "(1, 2, '3')\n",
      "{'keyword1': 4, 'keyword2': 'foo'}\n"
     ]
    }
   ],
   "source": [
    "#func() #直接调用 func() 函数会报错：func（）至少需要1个参数（给定0）\n",
    "#func(\"required argument\") # 调用 func() 函数的时候给定了一个参数，所以只会打印一个参数。\n",
    "#func(\"required argument\", 1, 2, '3') ## **args 接受了剩余的参数并放到了元组中\n",
    "func(\"required argument\", 1, 2, '3', keyword1=4, keyword2=\"foo\") # *args接受了无 Key 值的参数并放到元组中，**kwargs 接收了有 key 值的参数并放到了字典中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}